<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>moongose入门 | 七里香</title><meta name="keywords" content="mongoose"><meta name="author" content="william"><meta name="copyright" content="william"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="mongoose 介绍Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具。Mongoose 是 NodeJS 的驱动，不能作为其他语言的驱动。 Mongoose 有两个特点  通过关系型数据库的思想来设计非关系型数据库 基于 mongodb 驱动，简化操作  mongoose 的安装以及使用官网  安装  npm i mongoose --save 2">
<meta property="og:type" content="article">
<meta property="og:title" content="moongose入门">
<meta property="og:url" content="http://yokeney.gitee.io/my-hexo-blog/2021/10/15/moongose/index.html">
<meta property="og:site_name" content="七里香">
<meta property="og:description" content="mongoose 介绍Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具。Mongoose 是 NodeJS 的驱动，不能作为其他语言的驱动。 Mongoose 有两个特点  通过关系型数据库的思想来设计非关系型数据库 基于 mongodb 驱动，简化操作  mongoose 的安装以及使用官网  安装  npm i mongoose --save 2">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yokeney.gitee.io/my-hexo-blog/img/mongoose.png">
<meta property="article:published_time" content="2021-10-15T13:07:06.000Z">
<meta property="article:modified_time" content="2022-10-15T13:09:49.236Z">
<meta property="article:author" content="william">
<meta property="article:tag" content="mongoose">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yokeney.gitee.io/my-hexo-blog/img/mongoose.png"><link rel="shortcut icon" href="/my-hexo-blog/img/index.ico"><link rel="canonical" href="http://yokeney.gitee.io/my-hexo-blog/2021/10/15/moongose/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-hexo-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-hexo-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-hexo-blog/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'moongose入门',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-15 21:09:49'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-hexo-blog/img/2.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-hexo-blog/img/mongoose.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-hexo-blog/">七里香</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">moongose入门</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-15T13:07:06.000Z" title="发表于 2021-10-15 21:07:06">2021-10-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-10-15T13:09:49.236Z" title="更新于 2022-10-15 21:09:49">2022-10-15</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="moongose入门"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="mongoose-介绍"><a href="#mongoose-介绍" class="headerlink" title="mongoose 介绍"></a>mongoose 介绍</h1><p>Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具。Mongoose</p>
<p>是 <code>NodeJS</code> 的驱动，不能作为其他语言的驱动。</p>
<p><strong>Mongoose 有两个特点</strong></p>
<ol>
<li>通过关系型数据库的思想来设计非关系型数据库</li>
<li>基于 mongodb 驱动，简化操作</li>
</ol>
<h1 id="mongoose-的安装以及使用"><a href="#mongoose-的安装以及使用" class="headerlink" title="mongoose 的安装以及使用"></a>mongoose 的安装以及使用</h1><p><a target="_blank" rel="noopener" href="https://mongoosejs.com/">官网</a></p>
<ol>
<li>安装</li>
</ol>
<p><code>npm i mongoose --save</code></p>
<p>2、引入 mongoose 并连接数据库</p>
<p><code>const mongoose = require(&#39;mongoose&#39;);</code></p>
<p><code>mongoose.connect(&#39;mongodb://localhost/test&#39;);</code></p>
<p>如果有账户密码需要采用下面的连接方式：</p>
<p><code>mongoose.connect(&#39;mongodb://eggadmin:123456@localhost:27017/eggcms&#39;);</code></p>
<p>3、定义 Schema</p>
<p>数据库中的 Schema，为数据库对象的集合。schema 是 mongoose 里会用到的一种数据模式，<br>可以理解为表结构的定义；每个 schema 会映射到 mongodb 中的一个 collection，它不具备操作数据库的能力</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">UserSchema</span>=mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">name</span>: <span class="title class_">String</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">age</span>:<span class="title class_">Number</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">status</span>:<span class="string">&#x27;number&#x27;</span></span><br><span class="line"></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>4、创建数据模型</p>
<p>定义好了 Schema，接下就是生成 Model。model 是由 schema 生成的模型，可以对数据库的</p>
<p>操作。</p>
<blockquote>
<p>注意：<code>mongoose.model</code> 里面可以传入两个参数也可以传入三个参数<br>mongoose.model（参数 1:模型名称（首字母大写），参数 2:Schema）mongoose.model（参数 1:模型名称（首字母大写），参数 2:Schema，参数 3:数据库集合名称）</p>
</blockquote>
<p>如果传入 2 个参数的话:这个模型会和模型名称相同的复数的数据库建立连接：如通过下面方法创建模型，那么这个模型将会操作 users 这个集合。</p>
<p>如果传入 3 个参数的话:模型默认操作第三个参数定义的集合名称</p>
<p><code>var User=mongoose.model(&#39;User&#39;, UserSchema);</code></p>
<h3 id="查找数据"><a href="#查找数据" class="headerlink" title="查找数据"></a>查找数据</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">User</span>.<span class="title function_">find</span>(&#123;&#125;, <span class="keyword">function</span> (<span class="params">err, docs</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(docs)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="增加数据"><a href="#增加数据" class="headerlink" title="增加数据"></a>增加数据</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> u = <span class="keyword">new</span> <span class="title class_">User</span>(&#123;</span><br><span class="line">    <span class="comment">//实例化模型 传入增加的数据</span></span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;lisi2222333&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">    <span class="attr">status</span>: <span class="literal">true</span>,</span><br><span class="line">&#125;)</span><br><span class="line">u.<span class="title function_">save</span>()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="修改数据"><a href="#修改数据" class="headerlink" title="修改数据"></a>修改数据</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">User</span>.<span class="title function_">updateOne</span>(&#123; <span class="attr">name</span>: <span class="string">&#x27;lisi2222&#x27;</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;哈哈哈&#x27;</span> &#125;, <span class="keyword">function</span> (<span class="params">err, res</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">User</span>.<span class="title function_">deleteOne</span>(&#123; <span class="attr">_id</span>: <span class="string">&#x27;5b72ada84e284f0acc8d318a&#x27;</span> &#125;, <span class="keyword">function</span> (<span class="params">err</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// deleted at most one tank document</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="保存成功查找"><a href="#保存成功查找" class="headerlink" title="保存成功查找"></a>保存成功查找</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> u = <span class="keyword">new</span> <span class="title class_">User</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;lisi2222333&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">    <span class="attr">status</span>: <span class="literal">true</span>, <span class="comment">//类型转换</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">u.<span class="title function_">save</span>(<span class="keyword">function</span> (<span class="params">err, docs</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//console.log(docs);</span></span><br><span class="line">    <span class="title class_">User</span>.<span class="title function_">find</span>(&#123;&#125;, <span class="keyword">function</span> (<span class="params">err, docs</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(docs)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="mongoose-预定义模式修饰符"><a href="#mongoose-预定义模式修饰符" class="headerlink" title="mongoose 预定义模式修饰符"></a>mongoose 预定义模式修饰符</h2><p><code>lowercase</code>、<code>uppercase</code> 、<code>trim</code></p>
<p><code>mongoose</code> 提供的预定义模式修饰符，可以对我们增加的数据进行一些格式化。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">UserSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: &#123; <span class="attr">type</span>: <span class="title class_">String</span>, <span class="attr">trim</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    <span class="attr">age</span>: <span class="title class_">Number</span>,</span><br><span class="line">    <span class="attr">status</span>: &#123; <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="attr">default</span>: <span class="number">1</span> &#125;,</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="Mongoose-Getters-与-Setters-自定义修饰符"><a href="#Mongoose-Getters-与-Setters-自定义修饰符" class="headerlink" title="Mongoose Getters 与 Setters 自定义修饰符"></a>Mongoose Getters 与 Setters 自定义修饰符</h2><p>除了 mongoose 内置的修饰符以外，我们还可以通过 set（建议使用） 修饰符在增加数据的时候对数据进行格式化。也可以通过 get（不建议使用）在实例获取数据的时候对数据进行格式化。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">NewsSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">title</span>: <span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">    <span class="attr">author</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">pic</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">redirect</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">        <span class="title function_">set</span>(<span class="params">url</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!url) <span class="keyword">return</span> url</span><br><span class="line">            <span class="keyword">if</span> (url.<span class="title function_">indexOf</span>(<span class="string">&#x27;http://&#x27;</span>) != <span class="number">0</span> &amp;&amp; url.<span class="title function_">indexOf</span>(<span class="string">&#x27;https://&#x27;</span>) != <span class="number">0</span>) &#123;</span><br><span class="line">                url = <span class="string">&#x27;http://&#x27;</span> + url</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> url</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">content</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">status</span>: &#123; <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="attr">default</span>: <span class="number">1</span> &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> <span class="title class_">NewsSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">title</span>: <span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">    <span class="attr">author</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">pic</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">redirect</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">        <span class="title function_">set</span>(<span class="params">url</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!url) <span class="keyword">return</span> url</span><br><span class="line">            <span class="keyword">if</span> (url.<span class="title function_">indexOf</span>(<span class="string">&#x27;http://&#x27;</span>) != <span class="number">0</span> &amp;&amp; url.<span class="title function_">indexOf</span>(<span class="string">&#x27;https://&#x27;</span>) != <span class="number">0</span>) &#123;</span><br><span class="line">                url = <span class="string">&#x27;http://&#x27;</span> + url</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> url</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">get</span>: <span class="keyword">function</span> (<span class="params">url</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!url) <span class="keyword">return</span> url</span><br><span class="line">            <span class="keyword">if</span> (url.<span class="title function_">indexOf</span>(<span class="string">&#x27;http://&#x27;</span>) != <span class="number">0</span> &amp;&amp; url.<span class="title function_">indexOf</span>(<span class="string">&#x27;https://&#x27;</span>) != <span class="number">0</span>) &#123;</span><br><span class="line">                url = <span class="string">&#x27;http://&#x27;</span> + url</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> url</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">content</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">status</span>: &#123; <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="attr">default</span>: <span class="number">1</span> &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="Mongoose-索引"><a href="#Mongoose-索引" class="headerlink" title="Mongoose 索引"></a>Mongoose 索引</h2><p>索引是对数据库表中一列或多列的值进行排序的一种结构，可以让我们查询数据库变得更快。MongoDB 的索引几乎与传统的关系型数据库一模一样，这其中也包括一些基本的查询优化技巧。</p>
<p>mongoose 中除了以前创建索引的方式，我们也可以在定义 Schema 的时候指定创建索引。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">DeviceSchema</span> = <span class="keyword">new</span> mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">sn</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">Number</span>,</span><br><span class="line">        <span class="comment">// 唯一索引</span></span><br><span class="line">        <span class="attr">unique</span>: <span class="literal">true</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">name</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">        <span class="comment">// 普通索引</span></span><br><span class="line">        <span class="attr">index</span>: <span class="literal">true</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="Mongoose-内置-CURD"><a href="#Mongoose-内置-CURD" class="headerlink" title="Mongoose 内置 CURD"></a>Mongoose 内置 CURD</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Model</span>.<span class="title function_">deleteMany</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">deleteOne</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">find</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findById</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findByIdAndDelete</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findByIdAndRemove</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findByIdAndUpdate</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findOne</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findOneAndDelete</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findOneAndRemove</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">findOneAndUpdate</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">replaceOne</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">updateMany</span>()</span><br><span class="line"></span><br><span class="line"><span class="title class_">Model</span>.<span class="title function_">updateOne</span>()</span><br></pre></td></tr></table></figure>

<h2 id="扩展-Mongoose-CURD-方法"><a href="#扩展-Mongoose-CURD-方法" class="headerlink" title="扩展 Mongoose CURD 方法"></a>扩展 Mongoose CURD 方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;./db.js&#x27;</span>)</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">UserSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">age</span>: <span class="title class_">Number</span>,</span><br><span class="line">    <span class="attr">status</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">Number</span>,</span><br><span class="line"></span><br><span class="line">        <span class="attr">default</span>: <span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 静态方法</span></span><br><span class="line"><span class="title class_">UserSchema</span>.<span class="property">statics</span>.<span class="property">findByUid</span> = <span class="keyword">function</span> (<span class="params">uid, cb</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="title function_">find</span>(&#123; <span class="attr">_id</span>: uid &#125;, <span class="keyword">function</span> (<span class="params">err, docs</span>) &#123;</span><br><span class="line">        <span class="title function_">cb</span>(err, docs)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 实例方法</span></span><br><span class="line"><span class="title class_">UserSchema</span>.<span class="property">methods</span>.<span class="property">print</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;这是一个实例方法&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = mongoose.<span class="title function_">model</span>(<span class="string">&#x27;User&#x27;</span>, <span class="title class_">UserSchema</span>, <span class="string">&#x27;user&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Mongoose-校验参数"><a href="#Mongoose-校验参数" class="headerlink" title="Mongoose 校验参数"></a>Mongoose 校验参数</h2><table>
<thead>
<tr>
<th>参数</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>required</td>
<td>表示这个数据必须传入</td>
</tr>
<tr>
<td>max</td>
<td>用于 Number 类型数据，最大值</td>
</tr>
<tr>
<td>min</td>
<td>用于 Number 类型数据，最小值</td>
</tr>
<tr>
<td>enum</td>
<td>枚举类型，要求数据必须满足枚举值 enum: [‘0’, ‘1’, ‘2’]</td>
</tr>
<tr>
<td>match</td>
<td>增加的数据必须符合 match（正则）的规则</td>
</tr>
<tr>
<td>maxlength</td>
<td>最大值</td>
</tr>
<tr>
<td>minlength</td>
<td>最小值</td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">UserSchema</span> = <span class="keyword">new</span> mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: &#123; <span class="attr">type</span>: <span class="title class_">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    <span class="attr">age</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="comment">// 是否必须的校验器</span></span><br><span class="line">        <span class="attr">required</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="comment">// 数字类型的最大值校验器</span></span><br><span class="line">        <span class="attr">max</span>: <span class="number">120</span>,</span><br><span class="line">        <span class="comment">// 数字类型的最小值校验器</span></span><br><span class="line">        <span class="attr">min</span>: <span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">status</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">        <span class="comment">// 设置字符串的可选值</span></span><br><span class="line">        <span class="attr">enum</span>: [<span class="string">&#x27;0&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>],</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">phone</span>: &#123; <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="attr">match</span>: <span class="regexp">/^\d&#123;11&#125;$/</span> &#125;,</span><br><span class="line">    <span class="attr">desc</span>: &#123; <span class="attr">type</span>: <span class="title class_">String</span>, <span class="attr">maxlength</span>: <span class="number">20</span>, <span class="attr">minlength</span>: <span class="number">10</span> &#125;,</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="Mongoose-自定义的验证器"><a href="#Mongoose-自定义的验证器" class="headerlink" title="Mongoose 自定义的验证器"></a>Mongoose 自定义的验证器</h2><p>在缺省情况下创建的索引均不是唯一索引。下面的示例将创建唯一索引，如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">UserSchema</span> = <span class="keyword">new</span> mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: &#123; <span class="attr">type</span>: <span class="title class_">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    <span class="attr">age</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="comment">// 是否必须的校验器 required: true, // 数字类型的最大值校验器</span></span><br><span class="line">        <span class="attr">max</span>: <span class="number">120</span>, <span class="comment">// 数字类型的最小值校验器</span></span><br><span class="line">        <span class="attr">min</span>: <span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">status</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>, <span class="comment">// 设置字符串的可选值</span></span><br><span class="line">        <span class="attr">enum</span>: [<span class="string">&#x27;0&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>],</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">phone</span>: &#123; <span class="attr">type</span>: <span class="title class_">Number</span>, <span class="attr">match</span>: <span class="regexp">/^\d&#123;11&#125;$/</span> &#125;,</span><br><span class="line">    <span class="attr">desc</span>: &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>, <span class="comment">// 自定义的验证器，如果通过验证返回 true，没有通过则返回 false</span></span><br><span class="line">        <span class="attr">validate</span>: <span class="keyword">function</span> (<span class="params">desc</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> desc.<span class="property">length</span> &gt;= <span class="number">10</span></span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="MongoDB-聚合管道（Aggregation-Pipeline）"><a href="#MongoDB-聚合管道（Aggregation-Pipeline）" class="headerlink" title="MongoDB 聚合管道（Aggregation Pipeline）"></a>MongoDB 聚合管道（Aggregation Pipeline）</h2><p>使用聚合管道可以对集合中的文档进行变换和组合。</p>
<p>实际项目：表关联查询、数据的统计。</p>
<p>MongoDB 中使用 <code>db.COLLECTION_NAME.aggregate([&#123;&lt;stage&gt;&#125;,...]) </code>方法</p>
<p>来构建和使用聚合管道。先看下官网给的实例，感受一下聚合管道的用法。</p>
<h2 id="MongoDB-Aggregation-管道操作符与表达式"><a href="#MongoDB-Aggregation-管道操作符与表达式" class="headerlink" title="MongoDB Aggregation 管道操作符与表达式"></a>MongoDB Aggregation 管道操作符与表达式</h2><table>
<thead>
<tr>
<th>管道操作符</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>$project</td>
<td>增加、删除、重命名字段</td>
</tr>
<tr>
<td>$match</td>
<td>条件匹配。只满足条件的文档才能进入下一阶段</td>
</tr>
<tr>
<td>$limit</td>
<td>限制结果的数量</td>
</tr>
<tr>
<td>$skip</td>
<td>跳过文档的数量</td>
</tr>
<tr>
<td>$sort</td>
<td>条件排序。</td>
</tr>
<tr>
<td>$group</td>
<td>条件组合结果 统计</td>
</tr>
<tr>
<td>$lookup</td>
<td>操作符 用以引入其它集合的数</td>
</tr>
</tbody></table>
<h2 id="据-（表关联查询）SQL-和-NOSQL-对比"><a href="#据-（表关联查询）SQL-和-NOSQL-对比" class="headerlink" title="据 （表关联查询）SQL 和 NOSQL 对比:"></a>据 （表关联查询）SQL 和 NOSQL 对比:</h2><table>
<thead>
<tr>
<th>SQL</th>
<th>NO SQL</th>
</tr>
</thead>
<tbody><tr>
<td>WHERE</td>
<td>$match</td>
</tr>
<tr>
<td>GROUP BY</td>
<td>$group</td>
</tr>
<tr>
<td>HAVING</td>
<td>$match</td>
</tr>
<tr>
<td>SELECT</td>
<td>$project</td>
</tr>
<tr>
<td>ORDER BY</td>
<td>$sort</td>
</tr>
<tr>
<td>LIMIT</td>
<td>$limit</td>
</tr>
<tr>
<td>SUM()</td>
<td>$sum</td>
</tr>
<tr>
<td>COUNT()</td>
<td>$sum</td>
</tr>
<tr>
<td>join</td>
<td>$lookup</td>
</tr>
</tbody></table>
<p>管道表达式:</p>
<p>管道操作符作为“键”,所对应的“值”叫做管道表达式。</p>
<p>例如<code>&#123;$match:&#123;status:&quot;A&quot;&#125;&#125;</code>，<code>$match</code> 称为管道操作符，而 <code>status:&quot;A&quot;</code>称为管道表达式，</p>
<p>是管道操作符的操作数(Operand)。</p>
<p>每个管道表达式是一个文档结构，它是由字段名、字段值、和一些表达式操作符组成的。</p>
<table>
<thead>
<tr>
<th>常用表达式操作符</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>$addToSet</td>
<td>将文档指定字段的值去重</td>
</tr>
<tr>
<td>$max</td>
<td>文档指定字段的最大值</td>
</tr>
<tr>
<td>$min</td>
<td>文档指定字段的最小值</td>
</tr>
<tr>
<td>$sum</td>
<td>文档指定字段求和</td>
</tr>
<tr>
<td>$avg</td>
<td>文档指定字段求平均</td>
</tr>
<tr>
<td>$gt</td>
<td>大于给定值</td>
</tr>
<tr>
<td>$lt</td>
<td>小于给定值</td>
</tr>
<tr>
<td>$eq</td>
<td>等于给定值</td>
</tr>
</tbody></table>
<p><strong>数据模拟</strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">db.order.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;1&quot;</span>,<span class="string">&quot;uid&quot;</span>:10,<span class="string">&quot;trade_no&quot;</span>:<span class="string">&quot;111&quot;</span>,<span class="string">&quot;all_price&quot;</span>:100,<span class="string">&quot;all_num&quot;</span>:2&#125;)</span><br><span class="line"></span><br><span class="line">db.order.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;2&quot;</span>,<span class="string">&quot;uid&quot;</span>:7,<span class="string">&quot;trade_no&quot;</span>:<span class="string">&quot;222&quot;</span>,<span class="string">&quot;all_price&quot;</span>:90,<span class="string">&quot;all_num&quot;</span>:2&#125;)</span><br><span class="line"></span><br><span class="line">db.order.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;3&quot;</span>,<span class="string">&quot;uid&quot;</span>:9,<span class="string">&quot;trade_no&quot;</span>:<span class="string">&quot;333&quot;</span>,<span class="string">&quot;all_price&quot;</span>:20,<span class="string">&quot;all_num&quot;</span>:6&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;1&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;商品鼠标 1&quot;</span>,<span class="string">&quot;price&quot;</span>:50,num:1&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;1&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;商品键盘 2&quot;</span>,<span class="string">&quot;price&quot;</span>:50,num:1&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;1&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;商品键盘 3&quot;</span>,<span class="string">&quot;price&quot;</span>:0,num:1&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;2&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;牛奶&quot;</span>,<span class="string">&quot;price&quot;</span>:50,num:1&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;2&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;酸奶&quot;</span>,<span class="string">&quot;price&quot;</span>:40,num:1&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;3&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;矿泉水&quot;</span>,<span class="string">&quot;price&quot;</span>:2,num:5&#125;)</span><br><span class="line"></span><br><span class="line">db.order_item.insert(&#123;<span class="string">&quot;order_id&quot;</span>:<span class="string">&quot;3&quot;</span>,<span class="string">&quot;title&quot;</span>:<span class="string">&quot;毛巾&quot;</span>,<span class="string">&quot;price&quot;</span>:10,num:1&#125;)</span><br></pre></td></tr></table></figure>

<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3e7382cb5d03446fb0f4c9a1cf3cbfb8~tplv-k3u1fbpfcp-watermark.image" alt="截屏2022-08-29 21.14.37.png"></p>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e4ffde396e9f4bb7aecc54d0ad9d1a1d~tplv-k3u1fbpfcp-watermark.image" alt="截屏2022-08-29 21.15.22.png"></p>
<h3 id="project"><a href="#project" class="headerlink" title="$project"></a>$project</h3><p>修改文档的结构，可以用来重命名、增加或删除文档中的字段。要求查找 order 只返回文档中 <code>trade_no</code> 和 <code>all_price</code> 字段</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br></pre></td></tr></table></figure>

<h3 id="match"><a href="#match" class="headerlink" title="$match"></a>$match</h3><p><strong>作用</strong></p>
<p>用于过滤文档。用法类似于 find() 方法中的参数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$match</span>: &#123; <span class="attr">all_price</span>: &#123; <span class="attr">$gte</span>: <span class="number">90</span> &#125; &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="group"><a href="#group" class="headerlink" title="$group"></a>$group</h3><p>将集合中的文档进行分组，可用于统计结果。</p>
<p>统计每个订单的订单数量，按照订单号分组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order_item</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$group</span>: &#123; <span class="attr">_id</span>: <span class="string">&#x27;$order_id&#x27;</span>, <span class="attr">total</span>: &#123; <span class="attr">$sum</span>: <span class="string">&#x27;$num&#x27;</span> &#125; &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="sort"><a href="#sort" class="headerlink" title="$sort"></a>$sort</h3><p>将集合中的文档进行排序。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$match</span>: &#123; <span class="attr">all_price</span>: &#123; <span class="attr">$gte</span>: <span class="number">90</span> &#125; &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$sort</span>: &#123; <span class="attr">all_price</span>: -<span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="limit"><a href="#limit" class="headerlink" title="$limit"></a>$limit</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$match</span>: &#123; <span class="attr">all_price</span>: &#123; <span class="attr">$gte</span>: <span class="number">90</span> &#125; &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$sort</span>: &#123; <span class="attr">all_price</span>: -<span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$limit</span>: <span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="skip"><a href="#skip" class="headerlink" title="$skip"></a>$skip</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$match</span>: &#123; <span class="attr">all_price</span>: &#123; <span class="attr">$gte</span>: <span class="number">90</span> &#125; &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$sort</span>: &#123; <span class="attr">all_price</span>: -<span class="number">1</span> &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$skip</span>: <span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="lookup-表关联"><a href="#lookup-表关联" class="headerlink" title="$lookup 表关联"></a>$lookup 表关联</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$lookup</span>: &#123;</span><br><span class="line">            <span class="attr">from</span>: <span class="string">&#x27;order_item&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">localField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">foreignField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">as</span>: <span class="string">&#x27;items&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d8c2c327f8d1b360540&quot;</span>),</span><br><span class="line">    <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;uid&quot;</span>: <span class="number">10</span>,</span><br><span class="line">    <span class="string">&quot;trade_no&quot;</span>: <span class="string">&quot;111&quot;</span>,</span><br><span class="line">    <span class="string">&quot;all_price&quot;</span>: <span class="number">100</span>,</span><br><span class="line">    <span class="string">&quot;all_num&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="string">&quot;items&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d9c2c327f8d1b360543&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;商品鼠标 1&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da12c327f8d1b360544&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;商品键盘 2&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b74f457089f78dc8f0a4f3b&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;商品键盘 3&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">0</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125; &#123;</span><br><span class="line">    <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d902c327f8d1b360541&quot;</span>),</span><br><span class="line">    <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">    <span class="string">&quot;uid&quot;</span>: <span class="number">7</span>,</span><br><span class="line">    <span class="string">&quot;trade_no&quot;</span>: <span class="string">&quot;222&quot;</span>,</span><br><span class="line">    <span class="string">&quot;all_price&quot;</span>: <span class="number">90</span>,</span><br><span class="line">    <span class="string">&quot;all_num&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="string">&quot;items&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da52c327f8d1b360545&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;牛奶&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da92c327f8d1b360546&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;酸奶&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">40</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125; &#123;</span><br><span class="line">    <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d962c327f8d1b360542&quot;</span>),</span><br><span class="line">    <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;3&quot;</span>,</span><br><span class="line">    <span class="string">&quot;uid&quot;</span>: <span class="number">9</span>,</span><br><span class="line">    <span class="string">&quot;trade_no&quot;</span>: <span class="string">&quot;333&quot;</span>,</span><br><span class="line">    <span class="string">&quot;all_price&quot;</span>: <span class="number">20</span>,</span><br><span class="line">    <span class="string">&quot;all_num&quot;</span>: <span class="number">6</span>,</span><br><span class="line">    <span class="string">&quot;items&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743dad2c327f8d1b360547&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;3&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;矿泉水&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">2</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">5</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743dff2c327f8d1b360548&quot;</span>),</span><br><span class="line">            <span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;3&quot;</span>,</span><br><span class="line">            <span class="string">&quot;title&quot;</span>: <span class="string">&quot;毛巾&quot;</span>,</span><br><span class="line">            <span class="string">&quot;price&quot;</span>: <span class="number">10</span>,</span><br><span class="line">            <span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="多管道查询"><a href="#多管道查询" class="headerlink" title="多管道查询"></a><strong>多管道查询</strong></h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="property">order</span>.<span class="title function_">aggregate</span>([</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">$lookup</span>: &#123;</span><br><span class="line">            <span class="attr">from</span>: <span class="string">&#x27;order_item&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">localField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">foreignField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">            <span class="attr">as</span>: <span class="string">&#x27;items&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123; <span class="attr">$project</span>: &#123; <span class="attr">trade_no</span>: <span class="number">1</span>, <span class="attr">all_price</span>: <span class="number">1</span>, <span class="attr">items</span>: <span class="number">1</span> &#125; &#125;,</span><br><span class="line">    &#123; <span class="attr">$match</span>: &#123; <span class="attr">all_price</span>: &#123; <span class="attr">$gte</span>: <span class="number">90</span> &#125; &#125; &#125;,</span><br><span class="line">    &#123; <span class="attr">$sort</span>: &#123; <span class="attr">all_price</span>: -<span class="number">1</span> &#125; &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d8c2c327f8d1b360540&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;trade_no&quot;</span>: <span class="string">&quot;111&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;all_price&quot;</span>: <span class="number">100</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;items&quot;</span>: [&#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d9c2c327f8d1b360543&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;title&quot;</span>: <span class="string">&quot;商品鼠标 1&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line"></span><br><span class="line">&#125;, &#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da12c327f8d1b360544&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;title&quot;</span>: <span class="string">&quot;商品键盘 2&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line"></span><br><span class="line">&#125;, &#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b74f457089f78dc8f0a4f3b&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;title&quot;</span>: <span class="string">&quot;商 品键盘 3&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;price&quot;</span>: <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line"></span><br><span class="line">&#125;]</span><br><span class="line"></span><br><span class="line">&#125; &#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743d902c327f8d1b360541&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;trade_no&quot;</span>: <span class="string">&quot;222&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;all_price&quot;</span>: <span class="number">90</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;items&quot;</span>: [&#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da52c327f8d1b360545&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;title&quot;</span>: <span class="string">&quot;牛奶&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;price&quot;</span>: <span class="number">50</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;num&quot;</span>: <span class="number">1</span></span><br><span class="line"></span><br><span class="line">&#125;, &#123;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;_id&quot;</span>: <span class="title class_">ObjectId</span>(<span class="string">&quot;5b743da92c327f8d1b360546&quot;</span>),</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;order_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;title&quot;</span>: <span class="string">&quot;酸奶&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;price&quot;</span>: <span class="number">40</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;num&quot;</span>: <span class="number">1</span>&#125;]</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Mongoose-aggregate-多表关联查询"><a href="#Mongoose-aggregate-多表关联查询" class="headerlink" title="Mongoose aggregate 多表关联查询"></a>Mongoose aggregate 多表关联查询</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;./db.js&#x27;</span>)</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">OrderSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">order_id</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">uid</span>: <span class="title class_">Number</span>,</span><br><span class="line">    <span class="attr">trade_no</span>: <span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">all_price</span>: <span class="title class_">Number</span>,</span><br><span class="line">    <span class="attr">all_num</span>: <span class="title class_">Number</span>,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">OrderModel</span> = mongoose.<span class="title function_">model</span>(<span class="string">&#x27;Order&#x27;</span>, <span class="title class_">OrderSchema</span>, <span class="string">&#x27;order&#x27;</span>)</span><br><span class="line"><span class="title class_">OrderModel</span>.<span class="title function_">aggregate</span>(</span><br><span class="line">    [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">$lookup</span>: &#123;</span><br><span class="line">                <span class="attr">from</span>: <span class="string">&#x27;order_item&#x27;</span>,</span><br><span class="line"></span><br><span class="line">                <span class="attr">localField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">                <span class="attr">foreignField</span>: <span class="string">&#x27;order_id&#x27;</span>,</span><br><span class="line"></span><br><span class="line">                <span class="attr">as</span>: <span class="string">&#x27;item&#x27;</span>,</span><br><span class="line">            &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">    ],</span><br><span class="line">    <span class="keyword">function</span> (<span class="params">err, docs</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(docs)</span><br><span class="line">    &#125;</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="Mongoose-populate-关联查询"><a href="#Mongoose-populate-关联查询" class="headerlink" title="Mongoose populate 关联查询"></a>Mongoose populate 关联查询</h2><h3 id="定义-ref"><a href="#定义-ref" class="headerlink" title="定义 ref"></a>定义 ref</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title class_">ArticleSchema</span> = <span class="keyword">new</span> <span class="title class_">Schema</span>(&#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">title</span>:&#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>: <span class="title class_">String</span>, <span class="attr">unique</span>: <span class="literal">true</span>&#125;,</span><br><span class="line"></span><br><span class="line">cid : &#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>: <span class="title class_">Schema</span>.<span class="property">Types</span>.<span class="property">ObjectId</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">ref</span>:<span class="string">&#x27;ArticleCate&#x27;</span> <span class="comment">//model 的名称</span></span><br><span class="line"></span><br><span class="line">&#125;, <span class="comment">/*分类 id*/</span></span><br><span class="line"></span><br><span class="line"><span class="attr">author_id</span>:&#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>: <span class="title class_">Schema</span>.<span class="property">Types</span>.<span class="property">ObjectId</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">ref</span>:<span class="string">&#x27;User&#x27;</span></span><br><span class="line"></span><br><span class="line">&#125;, <span class="comment">/*用户的 id*/</span></span><br><span class="line"></span><br><span class="line"><span class="attr">author_name</span>:&#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>:<span class="title class_">String</span></span><br><span class="line"></span><br><span class="line">&#125;,</span><br><span class="line"></span><br><span class="line"><span class="attr">descripton</span>:<span class="title class_">String</span>,</span><br><span class="line"></span><br><span class="line">content : <span class="title class_">String</span></span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h3 id="关联查询"><a href="#关联查询" class="headerlink" title="关联查询"></a>关联查询</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">ArticleModel</span>.<span class="title function_">find</span>(&#123;&#125;).<span class="title function_">populate</span>(<span class="string">&#x27;cid&#x27;</span>).<span class="title function_">populate</span>(<span class="string">&#x27;author_id&#x27;</span>).<span class="title function_">exec</span>(<span class="keyword">function</span>(<span class="params">err,docs</span>)&#123;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(docs)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-hexo-blog/tags/mongoose/">mongoose</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-hexo-blog/2021/10/15/ts/"><img class="prev-cover" src="/my-hexo-blog/img/ts.png" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">TypeScript基础知识</div></div></a></div><div class="next-post pull-right"><a href="/my-hexo-blog/2021/06/01/Design/"><img class="next-cover" src="/my-hexo-blog/img/1.png" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">最常用的8大JavaScript设计模式，助你提高代码性能</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-hexo-blog/img/2.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">william</div><div class="author-info__description"> 青山不改，绿水长流</div></div><div class="card-info-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/yokeney"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/yokeney" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/my-hexo-blog/965660471@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">加油</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#mongoose-%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.</span> <span class="toc-text">mongoose 介绍</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#mongoose-%E7%9A%84%E5%AE%89%E8%A3%85%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">mongoose 的安装以及使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E6%95%B0%E6%8D%AE"><span class="toc-number">2.0.1.</span> <span class="toc-text">查找数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A2%9E%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="toc-number">2.0.2.</span> <span class="toc-text">增加数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE"><span class="toc-number">2.0.3.</span> <span class="toc-text">修改数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE"><span class="toc-number">2.0.4.</span> <span class="toc-text">删除数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%9D%E5%AD%98%E6%88%90%E5%8A%9F%E6%9F%A5%E6%89%BE"><span class="toc-number">2.0.5.</span> <span class="toc-text">保存成功查找</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#mongoose-%E9%A2%84%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%BC%8F%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-number">2.1.</span> <span class="toc-text">mongoose 预定义模式修饰符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-Getters-%E4%B8%8E-Setters-%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-number">2.2.</span> <span class="toc-text">Mongoose Getters 与 Setters 自定义修饰符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-%E7%B4%A2%E5%BC%95"><span class="toc-number">2.3.</span> <span class="toc-text">Mongoose 索引</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-%E5%86%85%E7%BD%AE-CURD"><span class="toc-number">2.4.</span> <span class="toc-text">Mongoose 内置 CURD</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%A9%E5%B1%95-Mongoose-CURD-%E6%96%B9%E6%B3%95"><span class="toc-number">2.5.</span> <span class="toc-text">扩展 Mongoose CURD 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-%E6%A0%A1%E9%AA%8C%E5%8F%82%E6%95%B0"><span class="toc-number">2.6.</span> <span class="toc-text">Mongoose 校验参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-%E8%87%AA%E5%AE%9A%E4%B9%89%E7%9A%84%E9%AA%8C%E8%AF%81%E5%99%A8"><span class="toc-number">2.7.</span> <span class="toc-text">Mongoose 自定义的验证器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MongoDB-%E8%81%9A%E5%90%88%E7%AE%A1%E9%81%93%EF%BC%88Aggregation-Pipeline%EF%BC%89"><span class="toc-number">2.8.</span> <span class="toc-text">MongoDB 聚合管道（Aggregation Pipeline）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MongoDB-Aggregation-%E7%AE%A1%E9%81%93%E6%93%8D%E4%BD%9C%E7%AC%A6%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">2.9.</span> <span class="toc-text">MongoDB Aggregation 管道操作符与表达式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8D%AE-%EF%BC%88%E8%A1%A8%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2%EF%BC%89SQL-%E5%92%8C-NOSQL-%E5%AF%B9%E6%AF%94"><span class="toc-number">2.10.</span> <span class="toc-text">据 （表关联查询）SQL 和 NOSQL 对比:</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#project"><span class="toc-number">2.10.1.</span> <span class="toc-text">$project</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#match"><span class="toc-number">2.10.2.</span> <span class="toc-text">$match</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#group"><span class="toc-number">2.10.3.</span> <span class="toc-text">$group</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#sort"><span class="toc-number">2.10.4.</span> <span class="toc-text">$sort</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#limit"><span class="toc-number">2.10.5.</span> <span class="toc-text">$limit</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#skip"><span class="toc-number">2.10.6.</span> <span class="toc-text">$skip</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#lookup-%E8%A1%A8%E5%85%B3%E8%81%94"><span class="toc-number">2.10.7.</span> <span class="toc-text">$lookup 表关联</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E7%AE%A1%E9%81%93%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.10.8.</span> <span class="toc-text">多管道查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mongoose-aggregate-%E5%A4%9A%E8%A1%A8%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.10.9.</span> <span class="toc-text">Mongoose aggregate 多表关联查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mongoose-populate-%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.11.</span> <span class="toc-text">Mongoose populate 关联查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-ref"><span class="toc-number">2.11.1.</span> <span class="toc-text">定义 ref</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.11.2.</span> <span class="toc-text">关联查询</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="VUE3集成企业级后台通用规范模版"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版">VUE3集成企业级后台通用规范模版</a><time datetime="2023-05-26T13:17:48.000Z" title="发表于 2023-05-26 21:17:48">2023-05-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解"><img src="/my-hexo-blog/img/react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="没看这篇文章之前原来我对react18一知半解"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解">没看这篇文章之前原来我对react18一知半解</a><time datetime="2023-04-24T09:11:02.000Z" title="发表于 2023-04-24 17:11:02">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="史上最全Vue3总结"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结">史上最全Vue3总结</a><time datetime="2023-04-24T09:01:19.000Z" title="发表于 2023-04-24 17:01:19">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析"><img src="/my-hexo-blog/img/vue_vs-react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="Vue与React18的区别深度解析"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析">Vue与React18的区别深度解析</a><time datetime="2023-04-24T08:39:32.000Z" title="发表于 2023-04-24 16:39:32">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件"><img src="/my-hexo-blog/img/babel.webp" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="手把手教你写babel插件"/></a><div class="content"><a class="title" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件">手把手教你写babel插件</a><time datetime="2022-10-25T01:10:14.000Z" title="发表于 2022-10-25 09:10:14">2022-10-25</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-hexo-blog/img/1.png')"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By william</div><div class="footer_custom_text">关山难越，谁悲失路之人</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-hexo-blog/js/utils.js"></script><script src="/my-hexo-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/my-hexo-blog/js/search/local-search.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="22712173" data-server="netease" data-mini="true" data-type="song" data-autoplay="false" data-lrcType="-1"> </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>